package com.company.cloud.common.tree;

import java.util.ArrayList;
import java.util.List;

public abstract class CloudTree <I,O extends TreeEntityModel>{

	
	  public abstract  boolean isTop(O entity);

	    public abstract  boolean isParent(O parentObj,O childObj);

	    public  abstract  O convert(I entity);

	    public List<O> tree(List<I> entitys){
	        List<O> resultList=new ArrayList<O>();
	        if(entitys!=null && entitys.size()>0){
	            List<O> objs=new ArrayList<O>();
	            for (I entity : entitys)  {
	                objs.add(this.convert(entity));
	            }
	            for (O obj : objs)  {
	                if(isTop(obj)){
	                    resultList.add(this.process(obj,objs));
	                }
	            }
	        }
	        return resultList;
	    }


	    private  O process(O parentObj, List<O> objs){
	        for (O obj : objs)  {
	            if(this.isParent(parentObj,obj)){
	                O childObj= this.process(obj,objs);
	                parentObj.addChild(childObj);
	            }
	        }
	        return parentObj;
	    }


}
